package softswitch.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import softswitch.model.RouteGroup;


@Stateless
public class RouteGroupDAO {

	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	
	public RouteGroup getById(Integer id){	
		return em.find(RouteGroup.class, id);
	}
	
    public RouteGroup save(RouteGroup routegroup) {
    	em.persist(routegroup); 
    	return routegroup;
    }
    
    public RouteGroup update(RouteGroup routegroup) {
    	return em.merge(routegroup);
    }
    
    public void delete(Integer id) {
    	em.remove(em.find(RouteGroup.class, id));	
    }
    
    @SuppressWarnings("unchecked")
    public List<RouteGroup> list(int first, int max) {    	
		Query query = em.createQuery("SELECT e FROM RouteGroup e ORDER by e.code ASC");
    	if (max > 0) {
            query.setMaxResults(max);
        }
        if (first > 0) {
            query.setFirstResult(first);
        }
        return query.getResultList();   
    }
    
	public RouteGroup getByCode(Integer code) {
		RouteGroup result = null;
    	Query query = em.createQuery("SELECT e FROM RouteGroup e WHERE e.code=:code");
    	query.setParameter("code", code);
		try {
			result = (RouteGroup) query.getSingleResult(); 
        }
    	catch (Exception e) {
		}
        return result; 
	}

    public RouteGroup getByName(String name) {
        RouteGroup result = null;
        Query query = em.createQuery("SELECT e FROM RouteGroup e WHERE e.name=:name");
        query.setParameter("name", name);
        try {
            result = (RouteGroup) query.getSingleResult();
        }
        catch (Exception e) {
        }
        return result;
    }

	
    
}

